package vip.ola.service.impl.code;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import vip.ola.core.common.Exception.ServiceException;
import vip.ola.core.common.util.RedisUtil;
import vip.ola.core.entity.code.CodeAccountHistory;
import vip.ola.core.entity.code.CodeUser;
import vip.ola.core.entity.code.PassageSettRecord;
import vip.ola.core.service.ISettRecordService;
import vip.ola.core.service.code.CodeAccountHistoryService;
import vip.ola.core.service.code.CodeUserService;
import vip.ola.core.service.code.PassageSettRecordService;
import vip.ola.service.dao.mapper.code.PassageSettRecordMapper;

import java.math.BigDecimal;
import java.time.LocalDateTime;

import static vip.ola.core.common.constant.CacheKey.ORDER_CONFIRM_LOCK_TIME;
import static vip.ola.core.common.constant.CacheKey.REMIT_LOCK;
import static vip.ola.core.common.constant.CodeConstant.*;
import static vip.ola.core.common.constant.Constant.SETT_STATUS_REMIT_SUCCESS;
import static vip.ola.core.common.constant.MchConstant.BIZ_ITEM_CAN_SETT_MONEY;
import static vip.ola.core.common.constant.RetEnum.CODE_ORDER_SAVE_HISTORY_ERROR;
import static vip.ola.core.common.constant.RetEnum.RET_COMM_REPETITIVE_OPERATION;


/**
 * <p>
 * 结算记录表 服务实现类
 * </p>
 *
 * @author faker
 * @since 2020-04-09
 */
@Transactional
@Service(interfaceClass = PassageSettRecordService.class, version = "1.0.0")
public class PassageSettRecordServiceImpl extends ServiceImpl<PassageSettRecordMapper, PassageSettRecord> implements PassageSettRecordService {

    @Autowired
    private CodeUserService codeUserService;
    @Autowired
    private CodeAccountHistoryService accountHistoryService;
    @Autowired
    private ISettRecordService settRecordService;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public IPage<PassageSettRecord> list(int currentPage, int pageSize, PassageSettRecord settRecord) {
        Page page = new Page(currentPage, pageSize);
        return baseMapper.list(page, settRecord);
    }

    @Override
    public boolean remit(PassageSettRecord settRecord) {
        // 加锁防止重复点击
        boolean lockSuc = redisUtil.lock(REMIT_LOCK + settRecord.getId(), ORDER_CONFIRM_LOCK_TIME);
        if (!lockSuc) {
            throw new ServiceException(RET_COMM_REPETITIVE_OPERATION);
        }
        CodeUser user = codeUserService.getById(settRecord.getPassageId());
        CodeAccountHistory history = new CodeAccountHistory();
        history.setUserId(user.getId());
        history.setUsername(user.getUsername());
        history.setAgentId(user.getAgentId());
        if(CODE_AGENT_TYPE.equals(user.getType())){
            history.setAgentId(user.getId());
        }
        history.setCodeBusId(user.getCodeBusId());
        if(CODE_CODE_SUB_TYPE.equals(user.getType()) || CODE_CODE_TYPE.equals(user.getType())){
            history.setCodeBusId(user.getId());
        }
        BigDecimal changeAmount = settRecord.getRemitAmount().add(user.getSettFee());
        if(SETT_STATUS_REMIT_SUCCESS.equals(settRecord.getStatus())){
            history.setOrderNo(settRecord.getSettOrderId());
            history.setBeforeAmount(user.getCanSettAmount());
            history.setAmount(BigDecimal.ZERO.subtract(changeAmount));
            history.setAfterAmount(user.getCanSettAmount().subtract(changeAmount));
            history.setOrderAmount(settRecord.getRemitAmount());
            history.setFee(user.getSettFee());
            history.setBizItem(BIZ_ITEM_CAN_SETT_MONEY);
            history.setBizType(TYPE_OUT);
            history.setFundDirection(FUND_SUB);
            history.setRemark(settRecord.getRemitRemark());
            history.setCreateTime(LocalDateTime.now());
            boolean isOk = accountHistoryService.save(history);
            if (!isOk) {
                throw new ServiceException(CODE_ORDER_SAVE_HISTORY_ERROR);
            }
        }
        codeUserService.updateSettAmount(settRecord.getPassageId(), settRecord.getStatus(), changeAmount);
        return super.updateById(settRecord);
    }
}


